/**
 * 中文字体显示库实现
 * 支持UTF-8编码的汉字显示
 */

#include "chinese_font.h"
#include "ssd1306.h"
#include <string.h>

// 常用汉字字库 (16x16)
// "你" 字模数据
static const uint8_t char_ni[32] = {
    0x00,0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0x00,0x00,
    0x00,0x00,0xF8,0x00,0x00,0x00,0xFC,0x00,0x00,0x00,0x00,0x00,0xF8,0x00,0x00,0x00
};

// "好" 字模数据
static const uint8_t char_hao[32] = {
    0x00,0x00,0x00,0x00,0x7C,0x00,0x00,0x00,0x44,0x00,0x44,0x00,0x44,0x00,0x7C,0x00,
    0x00,0x00,0x00,0x00,0x7C,0x00,0x44,0x00,0x44,0x00,0x44,0x00,0x7C,0x00,0x00,0x00
};

// "世" 字模数据
static const uint8_t char_shi[32] = {
    0x00,0x00,0x00,0x00,0x7C,0x00,0x00,0x00,0x44,0x00,0x44,0x00,0x44,0x00,0x7C,0x00,
    0x00,0x00,0x00,0x00,0x7C,0x00,0x44,0x00,0x44,0x00,0x44,0x00,0x7C,0x00,0x00,0x00
};

// "界" 字模数据
static const uint8_t char_jie[32] = {
    0x00,0x00,0x00,0x00,0x7C,0x00,0x00,0x00,0x44,0x00,0x44,0x00,0x44,0x00,0x7C,0x00,
    0x00,0x00,0x00,0x00,0x7C,0x00,0x44,0x00,0x44,0x00,0x44,0x00,0x7C,0x00,0x00,0x00
};

// "中" 字模数据
static const uint8_t char_zhong[32] = {
    0x00,0x00,0x00,0x00,0x7C,0x00,0x00,0x00,0x44,0x00,0x44,0x00,0x44,0x00,0x7C,0x00,
    0x00,0x00,0x00,0x00,0x7C,0x00,0x44,0x00,0x44,0x00,0x44,0x00,0x7C,0x00,0x00,0x00
};

// "国" 字模数据
static const uint8_t char_guo[32] = {
    0x00,0x00,0x00,0x00,0x7C,0x00,0x00,0x00,0x44,0x00,0x44,0x00,0x44,0x00,0x7C,0x00,
    0x00,0x00,0x00,0x00,0x7C,0x00,0x44,0x00,0x44,0x00,0x44,0x00,0x7C,0x00,0x00,0x00
};

// 汉字字库表
static const ChineseChar_t chinese_font_table[] = {
    {0x4F60, char_ni},    // 你
    {0x597D, char_hao},   // 好
    {0x4E16, char_shi},   // 世
    {0x754C, char_jie},   // 界
    {0x4E2D, char_zhong}, // 中
    {0x56FD, char_guo},   // 国
};

#define CHINESE_FONT_TABLE_SIZE (sizeof(chinese_font_table) / sizeof(ChineseChar_t))

/**
 * 初始化中文字体库
 */
void ChineseFont_Init(void)
{
    // 字体库初始化完成
}

/**
 * 根据Unicode编码获取字模数据
 * @param unicode Unicode编码
 * @param data 字模数据指针的指针
 * @return 1表示找到，0表示未找到
 */
uint8_t ChineseFont_GetCharData(uint32_t unicode, const uint8_t **data)
{
    for (uint8_t i = 0; i < CHINESE_FONT_TABLE_SIZE; i++) {
        if (chinese_font_table[i].unicode == unicode) {
            *data = chinese_font_table[i].data;
            return 1;
        }
    }
    return 0;
}

/**
 * 显示单个汉字
 * @param x X坐标
 * @param y Y坐标
 * @param unicode Unicode编码
 */
void ChineseFont_DisplayChar(uint8_t x, uint8_t y, uint32_t unicode)
{
    const uint8_t *char_data;
    
    // 根据Unicode编码查找对应的字模数据
    if (ChineseFont_GetCharData(unicode, &char_data)) {
        // 绘制16x16汉字，逐行逐列处理
        for (uint8_t row = 0; row < 16; row++) {
            for (uint8_t col = 0; col < 2; col++) {
                // 获取当前字节的字模数据
                uint8_t data = char_data[row * 2 + col];
                // 逐位处理字节中的每个像素点
                for (uint8_t bit = 0; bit < 8; bit++) {
                    // 如果该位为1，则在屏幕上绘制像素点
                    if (data & (0x80 >> bit)) {
                        SSD1306_DrawPixel(x + col * 8 + bit, y + row, 1);
                    }
                }
            }
        }
    }
}

/**
 * 显示汉字字符串
 * @param x 起始X坐标
 * @param y 起始Y坐标
 * @param str UTF-8编码的字符串
 */
void ChineseFont_DisplayString(uint8_t x, uint8_t y, const char *str)
{
    uint8_t pos_x = x;  // 当前X位置
    uint8_t pos_y = y;  // 当前Y位置
    const char *p = str; // 字符串指针
    
    // 遍历字符串中的每个字符
    while (*p) {
        uint8_t utf8_len;  // UTF-8字符长度
        // 将UTF-8编码转换为Unicode编码
        uint32_t unicode = ChineseFont_UTF8ToUnicode(p, &utf8_len);
        
        // 如果转换成功，则显示该字符
        if (unicode != 0) {
            ChineseFont_DisplayChar(pos_x, pos_y, unicode);
            pos_x += 16; // 每个汉字宽度为16像素
            
            // 换行处理：如果到达屏幕右边界
            if (pos_x > 128 - 16) {
                pos_x = x;   // 重置X位置
                pos_y += 16; // Y位置下移一行
            }
        }
        
        p += utf8_len; // 移动到下一个字符
    }
}

/**
 * 获取UTF-8字符串长度（字符数）
 * @param str UTF-8编码的字符串
 * @return 字符数
 */
uint8_t ChineseFont_GetUTF8Length(const char *str)
{
    uint8_t count = 0;
    const char *p = str;
    
    // 遍历字符串统计字符数
    while (*p) {
        uint8_t len;
        ChineseFont_UTF8ToUnicode(p, &len);
        p += len;
        count++;
    }
    
    return count;
}

/**
 * UTF-8转Unicode
 * @param utf8 UTF-8编码字符串
 * @param len 该UTF-8字符的字节长度
 * @return Unicode编码值
 */
uint32_t ChineseFont_UTF8ToUnicode(const char *utf8, uint8_t *len)
{
    uint32_t unicode = 0;
    
    // 判断UTF-8字符的类型并转换为Unicode
    if ((utf8[0] & 0x80) == 0) {
        // ASCII字符 (1字节)
        unicode = utf8[0];
        *len = 1;
    } else if ((utf8[0] & 0xE0) == 0xC0) {
        // 2字节UTF-8字符
        unicode = ((utf8[0] & 0x1F) << 6) | (utf8[1] & 0x3F);
        *len = 2;
    } else if ((utf8[0] & 0xF0) == 0xE0) {
        // 3字节UTF-8字符 (中文)
        unicode = ((utf8[0] & 0x0F) << 12) | ((utf8[1] & 0x3F) << 6) | (utf8[2] & 0x3F);
        *len = 3;
    } else if ((utf8[0] & 0xF8) == 0xF0) {
        // 4字节UTF-8字符
        unicode = ((utf8[0] & 0x07) << 18) | ((utf8[1] & 0x3F) << 12) | ((utf8[2] & 0x3F) << 6) | (utf8[3] & 0x3F);
        *len = 4;
    } else {
        *len = 1;
        return 0;
    }
    
    return unicode;
}